$(function(){
	var validator = $("#MobileRegisterModel").validate();
	// 验证规则，此验证规则会影响编辑器中JavaScript的的格式化操作
	$.validator.addRules($("#client_rules").html());
	$(".user_protocol").click(function() {
		if($.modal($(this))) {
			$.modal($(this)).show();
		} else {
			var modal = $.modal({
				title: "用户注册协议",
				trigger: $(this),
				content: $("#user_protocol").html()
			});
			modal.addButton({
				text: "同意协议并继续",
				click: function() {
					$("input[type='checkbox']").prop("checked", true);
					$("#btn_submit").removeAttr("disabled");
					this.hide();
				}
			});
		}
	});
	$('body').on('click', ".pwd-toggle", function() {
		var id = $(this).data("id");
		if ($('.pwd-toggle').hasClass('fa-eye')) {
			$('.pwd-toggle').removeClass('fa-eye');
			$('.pwd-toggle').addClass('fa-eye-slash');
			$('#' + id).attr("type", "password");
		} else {
			$('.pwd-toggle').addClass('fa-eye');
			$('.pwd-toggle').removeClass('fa-eye-slash');
			$('#' + id).attr("type", "text");
		}
	});
	
	$("#btn_submit").click(function() {
		if (!validator.form()) {
			return;
		}

		// 开始加载
		$.loading.start();
		var mobile=$('#mobileregistermodel-mobile').val();
		var password=$('#password').val();
		var sms_captcha=$('#mobileregistermodel-sms_captcha').val();
		$.post('/user/register_in', {
			mobile: mobile,
			password: password,
			sms_captcha: sms_captcha,
		}, function(result) {
			if(result.code==0){
				layer.alert(result.message)
			}
			if(result.code==1){
				layer.confirm(result.message, {
				  btn: ['继续浏览','前往首页'] //按钮
				}, function(){
				 	location.href=result.data;
				}, function(){
				  	location.href='/'
				});
			}
		}, "json");
		

		return false;
	});

	// 获取验证码链接点击事件
	$(".phonecode").click(function() {
		if (!$("#mobileregistermodel-mobile").valid()) {
			$("#mobileregistermodel-mobile").focus();
			return;
		}
		if (!$("#password").valid()) {
			$("#password").focus();
			return;
		}
		
		if ($(this).prop("disabled") == true || $(this).data("doing") == true) {
			return;
		}

		if ($(this).data("doing")) {
			return;
		}

		$(this).data("doing", true);

		// 获取短信验证码
		var target = this;

		var mobile = $("#mobileregistermodel-mobile").val();
	
		$.post('/user/sms_captcha', {
			mobile: mobile,
		}, function(result) {
			if (result.code == 0) {
				// 开始倒计时
				countdown(target, "获取短信验证码");
			} else {
				if (result.code == 1) {
					$.validator.showError($("#mobileregistermodel-sms_captcha"), result.message);
				} else {
					$.msg(result.message, {
						time: 5000
					});
				}
				// 失败后点击验证码
				if ($("#captcha-image").size() > 0) {
					$("#captcha").val("");
					$("#captcha-image").click();
				}
			}
			$(target).data("doing", false);
		}, "json");
	});
	var wait = 60;
	function countdown(obj, msg) {
		obj = $(obj);

		if (wait <= 0) {
			obj.prop("disabled", false);
			obj.html(msg);
			wait = 60;
		} else {
			if (msg == undefined || msg == null) {
				msg = obj.html();
			}
			obj.prop("disabled", true);
			obj.html(wait + "秒后重新获取");
			wait--;
			setTimeout(function() {
				countdown(obj, msg)
			}, 1000)
		}
	}
	
	

})
